package com.snow.common.base;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import java.io.Serializable;

/**
 * 接口响应对象
 *
 * @author 周成成
 * @create 2017-07-04 下午2:35
 **/
@Data
@ApiModel
public class R<T> implements Serializable {

    private static final long serialVersionUID = -8705057226888494469L;

    private boolean success;
    @ApiModelProperty(value = "数据体")
    private T data;
    @ApiModelProperty(value = "响应状态码")
    private String code;
    @ApiModelProperty(value = "响应信息")
    private String message;

    public R() {
    }

    public R(T result) {
        this.success = true;
        this.data = result;
        this.code = ResponseCode.SUCCESS_200.getCode();
    }

    public R(T result, String msg) {
        this.success = true;
        this.data = result;
        this.code = ResponseCode.SUCCESS_200.getCode();
        this.message = msg;
    }

    public R(boolean flag, String msg) {
        if (flag) {
            this.code = ResponseCode.SUCCESS_200.getCode();
        } else {
            this.message = ResponseCode.NOT_ACCEPTABLE_406.getCode();
        }
        this.success = flag;
        this.message = msg;
    }

    public R(ResponseCode responseCode, String msg) {
        this.code = responseCode.getCode();
        this.message = msg;
    }

    public static R build(ResponseCode responseCode, String msg) {
        return new R(responseCode, msg);
    }

    public static R ok(Object object) {
        return new R(object);
    }

    public static R ok() {
        return new R(null);
    }

    public static R fail(String msg) {
        return new R(false, msg);
    }

    public static R stockLuck() {
        R resultBean = new R();
        resultBean.setCode("200");
        resultBean.setMessage("有业务员正在进行盘点操作，请等待盘点结束后，再进行出入库等相关业务操作作");
        resultBean.setData(false);
        return resultBean;
    }


    public static R noStock() {
        R resultBean = new R();
        resultBean.setCode("200");
        resultBean.setMessage(" 库存不足,请核对库存");
        resultBean.setData(false);
        return resultBean;
    }

    public static R existOrder() {
        R resultBean = new R();
        resultBean.setCode("200");
        resultBean.setMessage(" 当前订单有关联退货单，不可作废");
        resultBean.setData(false);
        return resultBean;
    }

}
